import { AssetManager, assetManager, Prefab } from 'cc';

const getBundle = async (bundleName: string): Promise<AssetManager.Bundle> => {
    return new Promise((resolve, reject) => {
        let bundle = assetManager.getBundle(bundleName);
        if (bundle) {
            resolve(bundle);
        } else {
            assetManager.loadBundle(bundleName, (err, bundle) => {
                if (err) {
                    reject(err);
                } else {
                    resolve(bundle);
                }
            });
        }
    });
};

// 动态资源加载
export const loadBundle = async (bundleName: string, path: string, typ = Prefab) => {
    let bundle = await getBundle(bundleName);
    return new Promise((resolve, reject) => {
        bundle.load(path, typ, (err, asset) => {
            if (err) {
                reject(err);
            } else {
                resolve(asset);
            }
        });
    });
};
